/**
 * @mbg.generated generator on Sun Apr 09 20:13:46 CST 2023
 */
package com.ybox.crawler.service.impl;

import com.mysql.cj.jdbc.exceptions.MysqlDataTruncation;
import com.ybox.common.core.text.HMStringUtils;
import com.ybox.crawler.domain.pojos.AppLabel;
import com.ybox.crawler.domain.pojos.AppLabelExample;
import com.ybox.crawler.mapper.AppLabelMapper;
import com.ybox.crawler.service.AppLabelService;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.xml.crypto.Data;

/**
 * @author 16554
 */
@Service
public class AppLabelServiceImpl implements AppLabelService {
    private final AppLabelMapper appLabelMapper;

    private static final Logger log = LoggerFactory.getLogger(AppLabelServiceImpl.class);

    public AppLabelServiceImpl(AppLabelMapper appLabelMapper) {
        this.appLabelMapper = appLabelMapper;
    }

    /**
     * countByExample
     * @param example example
     * @return long
     */
    @Override
    public long countByExample(AppLabelExample example) {
        return appLabelMapper.countByExample(example);
    }

    /**
     * deleteByExample
     * @param example example
     * @return int
     */
    @Override
    public int deleteByExample(AppLabelExample example) {
        return appLabelMapper.deleteByExample(example);
    }

    /**
     * deleteByPrimaryKey
     * @param id id
     * @return int int
     */
    @Override
    public int deleteByPrimaryKey(Integer id) {
        return appLabelMapper.deleteByPrimaryKey(id);
    }

    /**
     * insert
     * @param row row
     * @return int int
     */
    @Override
    public int insert(AppLabel row) {
        return appLabelMapper.insert(row);
    }

    /**
     * insertSelective
     * @param row row
     * @return int int
     */
    @Override
    public int insertSelective(AppLabel row) {
        return appLabelMapper.insertSelective(row);
    }

    /**
     * selectByExample
     * @param example example
     * @return List<AppLabel> List<AppLabel>
     */
    @Override
    public List<AppLabel> selectByExample(AppLabelExample example) {
        return appLabelMapper.selectByExample(example);
    }

    /**
     * selectByPrimaryKey
     * @param id id
     * @return AppLabel AppLabel
     */
    @Override
    public AppLabel selectByPrimaryKey(Integer id) {
        return appLabelMapper.selectByPrimaryKey(id);
    }

    /**
     * updateByExampleSelective
     * @param row row
     * @param example example
     * @return int int
     */
    @Override
    public int updateByExampleSelective(AppLabel row, AppLabelExample example) {
        return appLabelMapper.updateByExampleSelective(row, example);
    }

    /**
     * updateByExample
     * @param row row
     * @param example example
     * @return int int
     */
    @Override
    public int updateByExample(AppLabel row, AppLabelExample example) {
        return appLabelMapper.updateByExample(row, example);
    }

    /**
     * updateByPrimaryKeySelective
     * @param row row
     * @return int int
     */
    @Override
    public int updateByPrimaryKeySelective(AppLabel row) {
        return appLabelMapper.updateByPrimaryKeySelective(row);
    }

    /**
     * updateByPrimaryKey
     * @param row row
     * @return int int
     */
    @Override
    public int updateByPrimaryKey(AppLabel row) {
        return appLabelMapper.updateByPrimaryKey(row);
    }

    @Override
    public int updateBatch(List<AppLabel> list) {
        return appLabelMapper.updateBatch(list);
    }

    @Override
    public int updateBatchSelective(List<AppLabel> list) {
        return appLabelMapper.updateBatchSelective(list);
    }


    /**
     * @param labels   从页面爬取的标签  多个的时候，以逗号分隔
     * @return  标签id  多个以逗号分隔
     */
    @Override
    public String getLabelIds(String labels){
        long currentTimeMillis = System.currentTimeMillis();
        log.info("获取channel信息，标签:labels:{}", labels);
        List<AppLabel> appLabelList = new ArrayList<>();
        if (StringUtils.isNotEmpty(labels)) {
            //转换成小写
            labels = labels.toLowerCase();
            List<String> tmpLabels = Arrays.asList(labels.split(","));
            tmpLabels = new ArrayList<>(tmpLabels);
            appLabelList = appLabelMapper.queryAppCollectLabelByLabels(tmpLabels);
            if (null != appLabelList && !appLabelList.isEmpty()) {
                appLabelList = addLabelList(tmpLabels, appLabelList);
            } else {
                // 为空则插入
                appLabelList = addLabelList(tmpLabels);
            }
        }
        List<String> labelList = appLabelList.stream().map(label -> HMStringUtils.toString(label.getId())).collect(Collectors.toList());
        String resultStr = HMStringUtils.listToStr(labelList, ",");
        log.info("获取label信息完成，标签:{},labelIds:{},耗时:{}", labels, resultStr, System.currentTimeMillis() - currentTimeMillis);
        return resultStr;
    }

    /**
     * 过滤保存
     *
     * @param tmpLabels
     * @param adLabelList
     * @return
     */
    private List<AppLabel> addLabelList(List<String> tmpLabels, List<AppLabel> adLabelList) {
        if(tmpLabels!=null && !tmpLabels.isEmpty()){
            for(AppLabel adLabel : adLabelList){
                for (int i = 0;i<tmpLabels.size();i++) {
                    if(tmpLabels.get(i).contains(adLabel.getName())){
                        tmpLabels.remove(i);
                    }
                }
            }
        }
        if(tmpLabels!=null && !tmpLabels.isEmpty()){
            adLabelList.addAll(addLabelList(tmpLabels));
        }

        return adLabelList;

    }

    /**
     * 保存
     * @param tmpLabels
     * @return
     */
    private List<AppLabel> addLabelList(List<String> tmpLabels) {
        List<AppLabel> appLabelList = new ArrayList<>();
        for (String label : tmpLabels) {
            appLabelList.add(addLabel(label));
        }
        // 插入时，由于数据库已经存在了，可能返回null值，所以此处要去除null值
        appLabelList.removeAll(Collections.singleton(null));
        return appLabelList;
    }


    /**
     * 自定义保存
     * @param label
     * @return
     */
    private AppLabel addLabel(String label) {
        ArrayList<String> strings = new ArrayList<>();
        // 去除特殊字符
        strings.add(com.ybox.common.core.utils.StringUtils.stripSpecialCharacters(label));
        List<AppLabel> appLabels = appLabelMapper.queryAppCollectLabelByLabels(strings);
        if (null != appLabels && !appLabels.isEmpty()){
            return null;
        }
        AppLabel appLabel = new AppLabel();
        // 去除特殊字符
        label = com.ybox.common.core.utils.StringUtils.stripSpecialCharacters(label);
        appLabel.setName(label.length() > 20? label.substring(0,20):label);
        appLabel.setType((byte) 0); //0系统增加 1人工增加
        appLabel.setCreateTime(new Date());
        appLabelMapper.insertSelective(appLabel);
        return appLabel;
    }
}